<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


// ------------------------------------------------------------------------

/**
 * Announcement class
 *
 * @category	Admin
 */
class AnnouncementAction extends AdminAction
{

	protected $Announcement;


	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->Announcement = D('Announcement');
	}


	//公告列表
	public function announcementList()
	{
	    import('ORG.Util.Page');

		$order = array('announce_id' => 'desc');
		$total = $this->Announcement->count();
		$Page = new Page($total);
		$pagination = $Page->show();

		$announceList = $this->Announcement->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('pagination', $pagination);
    	$this->assign('announceList', $announceList);

    	$this->display();
	}


	//公告批量管理
	public function manageAnnouncement()
	{
		if ($this->isPost())
		{
			$displayOrder = $_POST['display_order'];
			$mods = $_POST['mods'];
		    if (!empty($displayOrder))
		    {
				foreach ($displayOrder as $key => $value)
				{
					$data = array('display_order' => intval($value));
					$map = array('announce_id' => $key);
					$this->Announcement->where($map)->save($data);
				}
			}
			if (!empty($mods))
			{
				$map = array('announce_id' => array('in', $mods));
				$this->Announcement->where($map)->delete();
			}

			$this->Announcement->cacheAnnouncements();

			$this->success(L('announce.announce_updated_succeed'));
		}
		else
		{
		    $this->redirect('Admin-Announcement/announcementList');
		}
	}


	//添加公告
	public function addAnnouncement()
	{
		$this->editAnnouncement();
	}


	//编辑公告
	public function editAnnouncement()
	{
	    $announceID = intval($_REQUEST['announce_id']);
	    $announce = $this->Announcement->find($announceID);

		if ($this->isPost())
		{
		    if (strtotime($_POST['start_time']) > strtotime($_POST['expire_time']))
		    {
		    	$this->error(L('announce.expire_time_invalid'));
		    }
		    $data = array(
		        'subject'      => trim($_POST['subject']),
		        'message'      => trim($_POST['message']),
		        'target_group' => implode(',', $_POST['target_group']),
		        'start_time'   => intval(strtotime($_POST['start_time'])),
		        'expire_time'  => intval(strtotime($_POST['expire_time'])),
		    );
			if (!empty($announce))
			{
			    $map = array('announce_id' => $announceID);
				$this->Announcement->where($map)->save($data);
				$message = L('announce.announce_edited_succeed');
			}
			else
			{
			    $data['author_id'] = $this->_admin['user_id'];
		        $data['author_name'] = $this->_admin['username'];
			    $this->Announcement->add($data);
			    $message = L('announce.announce_added_succeed');
			}

			$this->Announcement->cacheAnnouncements();

            $this->assign('jumpUrl', __URL__.'/announcementList');
            $this->success($message);
		}
		else
		{
		    $userGroupList = M('UserGroup')->select();
		    $this->assign('userGroupList', $userGroupList);

		    $announce['start_time']  = empty($announceID) ? date('Y-m-d') : date('Y-m-d', $announce['start_time']);
		    $announce['expire_time'] = empty($announceID) ? date('Y-m-d', time() + 86400 * 7) : date('Y-m-d', $announce['expire_time']);
			$this->assign('rs', $announce);

			$this->display('editAnnouncement');
		}
	}

}
// End Class

/* End of file AnnouncementAction.class.php */
/* Location: App/Lib/Action/Admin/AnnouncementAction.class.php */